import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  { path: '/reg', component: () => import('@/views/Reg.vue') },
  {
    path: '/',
    redirect: '/home',
    component: () => import('@/views/Main.vue'),
    children: [
      { path: 'art-cate', component: () => import('@/views/Menu/Artcate/ArtCate.vue') },
      { path: 'home', component: () => import('@/views/Menu/Home/Home.vue') },
      { path: 'user-info', component: () => import('@/views/Menu/User/UserInfo.vue') },
      { path: 'user-pwd', component: () => import('@/views/Menu/User/UserPwd.vue') },
      { path: 'user-avatar', component: () => import('@/views/Menu/User/UserAvatar.vue') },
      { path: 'art-list', component: () => import('@/views/Menu/Artcate/ArtList.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 导航守卫
router.beforeEach((to, from, next) => {
  const token = store.state.user.token

  if (to.path === '/login' || to.path === '/reg') {
    next()
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
